<template>
  <div>
    <div class="mod-config" v-if="visible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.key" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button v-if="isAuth('interview:interviewRecords:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="isAuth('interview:interviewRecords:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        stripe
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          type="index"
          header-align="center"
          align="center"
          width="50"
          label="序号" fixed="left">
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          label="姓名">
        </el-table-column>
        <el-table-column
          prop="phone"
          header-align="center"
          align="center"
          label="电话"
          width="110">
        </el-table-column>
        <el-table-column
          prop="nativePlace"
          header-align="center"
          align="center"
          label="籍贯">
        </el-table-column>
        <el-table-column
          prop="minzu"
          header-align="center"
          align="center"
          label="民族">
        </el-table-column>
        <el-table-column
          prop="age"
          header-align="center"
          align="center"
          label="年龄"
          width="50">
        </el-table-column>
        <el-table-column
          prop="gender"
          header-align="center"
          align="center"
          label="性别"
          width="60">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.gender == 1" >男</el-tag>
            <el-tag v-else type="danger">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="workYear"
          header-align="center"
          align="center"
          label="工龄"
          width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.workYear == 0">无</span>
            <span v-else-if="scope.row.workYear == 1">一年</span>
            <span v-else-if="scope.row.workYear == 2">两年</span>
            <span v-else-if="scope.row.workYear == 3">三年</span>
            <span v-else>三年以上</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="appointmentTime"
          header-align="center"
          align="center"
          label="预约时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="station"
          header-align="center"
          align="center"
          label="应聘岗位"
          width="150">
        </el-table-column>
        <el-table-column
          prop="interviewer"
          header-align="center"
          align="center"
          label="面试官"
          width="150">
          <template slot-scope="scope">
            <span>{{getInterviewName (scope.row.interviewer)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="flowStatus"
          header-align="center"
          align="center"
          label="当前面试流程状态"
          width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.flowStatus == 0">等待录入结果</el-tag>
            <el-tag v-else-if="scope.row.flowStatus == 1" type="info">面试未通过</el-tag>
            <el-tag v-else-if="scope.row.flowStatus == 2" type="warning">等待录取审核</el-tag>
            <el-tag v-else-if="scope.row.flowStatus == 3" type="danger">完成录取审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="recordDetails(scope.row)">详情</el-button>
            <el-button v-if="isAuth('interview:interviewRecords:addInterviewResult')" type="text" size="small" @click="resultDetails(scope.row.id)">录入结果</el-button>
            <el-button v-if="isAuth('interview:interviewRecords:checkResult')" type="text" size="small" @click="checkDetails(scope.row.id)">录取审核</el-button>
            <el-button v-if="isAuth('interview:interviewRecords:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="isAuth('interview:interviewRecords:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
    <!--    详情页面-->
    <record-details ref="recordDetails" @goBack="goback()" v-if="detailVisible"></record-details>
    <!--    录入结果页面-->
    <result-details ref="resultDetails" @remove="remove()" v-if="resultVisible" @refreshDataList="getDataList"></result-details>
    <!--    录取审核页面-->
    <check-details ref="checkDetails" @gomove="gomove()" v-if="checkVisible" @refreshDataList="getDataList"></check-details>
  </div>
</template>

<script>
import AddOrUpdate from './interviewrecords-add-or-update.vue'
import recordDetails from "./recordDetails.vue"
import resultDetails from "./resultDetails.vue";
import checkDetails from "./checkDetails.vue";
export default {
  data () {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      detailVisible: false,
      resultVisible: false,
      checkVisible: false,
      visible: true,
      interviewerList: [],
    }
  },
  components: {
    checkDetails,
    resultDetails,
    recordDetails,
    AddOrUpdate
  },
  activated() {
    this.getDataList()
    this.getInterviewer()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/interview/interviewRecords/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/interview/interviewRecords/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    // 跳转至详情页面
    recordDetails(row) {
      this.visible = false
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.recordDetails.init(row)
      })
    },
    // 详情组件跳转回父组件
    goback() {
      this.detailVisible = false
      this.visible = true
    },



    // 跳转录入结果组件
    resultDetails(id) {
      this.visible = false
      this.resultVisible = true
      this.$nextTick(() => {
        this.$refs.resultDetails.init(id)
      })
    },
    // 录入结果页面跳回父组件
    remove() {
      this.resultVisible = false
      this.visible = true
    },
    // 跳转录取审核组件
    checkDetails(id) {
      this.visible = false
      this.checkVisible = true
      this.$nextTick(() => {
        this.$refs.checkDetails.init(id)
      })
    },
    // 录取审核页面跳回父组件
    gomove() {
      this.checkVisible = false
      this.visible = true
    },

    // 获取面试官姓名集合
    getInterviewer() {
      this.$http({
        url: this.$http.adornUrl(`/sys/user/getInterviewer`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.interviewerList = data.interviewers.map(item => ({
            value: item.userId,
            label: item.username
          }))
          console.log(this.interviewerList)
        }
      })
    },
    getInterviewName(interviewer) {
      const interview = this.interviewerList.find(item => item.value === interviewer)
      return interview ? interview.label : '张三'
    }
  }
}
</script>


